package com.jswlw.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    private static String DATE_PARTEN = "yyyy-MM-dd";

    private static String DATE_PARTEN_CH = "yyyy年MM月dd日hh时mm分ss秒";

    public static Date stringtoDate(String dateStr, String format) {
        Date d = null;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr);
        } catch (Exception e) {
            // log.error(e);
            d = null;
        }
        return d;
    }

    public static String nowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PARTEN);
        return sdf.format(new Date());
    }

    public static Date addMinute(Integer minute) {
        long curr = System.currentTimeMillis();
        long next = curr + minute * 60 * 1000;
        return new Date(next);
    }

    public static String chDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PARTEN_CH);
        return sdf.format(new Date());
    }

    public static String monthFirst() {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PARTEN);
        return sdf.format(cale.getTime());
    }

    public static String monthFirst(int sub) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, sub);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PARTEN);
        return sdf.format(cale.getTime());
    }
}
